<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>仓库管理</el-breadcrumb-item>
      <el-breadcrumb-item>出库管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!--        查询框-->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入货物内容"
                    clearable
          >
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="openEnter">新建出库</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading"
                :data="outList"
                border
                height="490"
                :default-sort="{prop: 'date', order: 'descending'}"
                stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="RFID编号" prop="num" width="170px" sortable></el-table-column>
        <el-table-column label="订单号" prop="orderNum" width="170px" sortable></el-table-column>
        <el-table-column label="货物名称" prop="name" sortable></el-table-column>
        <el-table-column label="存储数量" prop="count" sortable></el-table-column>
        <el-table-column label="客户名" prop="client.name"></el-table-column>
        <el-table-column label="联系方式" prop="client.phone"></el-table-column>
        <el-table-column label="入库时间" prop="date" width="150px" sortable></el-table-column>
        <el-table-column label="操作员" prop="operator"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip effect="dark" :content="scope.row.expNo===null?'发货':'物流信息'" placement="top"
                        :enterable="false">
              <el-button :type="scope.row.expNo===null?'warning':'success'"
                         :icon="scope.row.expNo===null?'el-icon-box':'el-icon-truck'" size="mini"
                         @click="openShip(scope.row)">
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!--        出库登记-->
    <el-dialog
      title="出库登记"
      :visible.sync="enterDialog"
      width="45%"
      @close="enterClose">
      <el-form
        :model="enter"
        ref="enterRef"
        :rules="enterRules"
        label-width="90px"
        :inline="true">
        <el-form-item label="出库类型">
          <el-select v-model="enter.type"
                     placeholder="请选择您的入库方式">
            <el-option
              v-for="item in options1"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="enter.type===1" label="订单号">
          <el-input v-model="enter.orderNum" style="width: 220px"></el-input>
        </el-form-item>
        <el-form-item label="产品名称">
          <el-input v-model="enter.name" :disabled="enter.type===''" style="width: 220px"></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input v-model="enter.count" :disabled="enter.type===''" style="width: 220px"></el-input>
        </el-form-item>
        <el-divider></el-divider>
        <el-form-item v-if="enter.type===1" label="RFID编号">
          <el-autocomplete
            v-model="enter.area1"
            :fetch-suggestions="querySearchAsync1"
            placeholder="请扫描RFID标签"
            @select="handleSelect1">
          </el-autocomplete>
        </el-form-item>
      </el-form>
      <el-form
        :model="ship"
        ref="shipRef"
        label-width="90px"
      >
        <p style="color: red">*请认真填写订单号</p>
        <el-form-item label="快递公司">
          <el-select v-model="ship.expCode" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="快递单号">
          <el-input v-model="ship.expNo"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
                <el-button @click="enterDialog = false ,shipDialog = false">取 消</el-button>
                <el-button type="primary" @click="shipYes1">确 定</el-button>
            </span>
    </el-dialog>
    <!--创建快递-->
    <el-dialog
      title="出库"
      :visible.sync="shipDialog"
      width="30%"
    >
      <el-form
        :model="ship"
        ref="shipRef"
        label-width="90px"
      >
        <p style="color: red">*请认真填写订单号</p>
        <el-form-item label="快递公司">
          <el-select v-model="ship.expCode" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="快递单号">
          <el-input v-model="ship.expNo"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
                <el-button @click="shipDialog = false" type="info">关 闭</el-button>
                <el-button type="primary" @click="shipYes">确 定</el-button>
            </span>
    </el-dialog>
    <!--物流信息-->
    <el-dialog
      title="物流信息"
      :visible.sync="logisticsDialog"
      width="40%"
    >
      <el-timeline :reverse="reverse">
        <el-timeline-item
          v-for="(kd, index) in kd.Traces"
          :key="index"
          :timestamp="kd.AcceptTime">
          {{ kd.AcceptStation }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      outList: [],
      loading: false,
      shipDialog: false,
      enterDialog: false,
      queryInfo: {
        name: '',
        state: 3
      },
      queryInfo1: {
        name: '',
        state: 2,
        isread:1
      },
      enter: {
        tNote: '',
        tid: '',
        orderNum: '',
        name: '',
        type: '',
        count: '',
        countQ: 0,
        countD: 0,
        pid: [],
        area1: '',
        area2: 0,
        lid: '',
        operator: this.$store.state.user.name
      },
      enterRules: {
        universal: [{required: true, message: '必填项', trigger: 'blur'}],
      },
      ship: {
        id: '',
        area1: '',
        expCode: '',
        expNo: '',
        rfid:''
      },
      options1: [
        {
          value: 1,
          label: '货物出库',
        },
        {
          value: 2,
          label: '退货出库',
          disabled: true
        },
        {
          value: 3,
          label: '废物出库',
          disabled: true
        },

      ],
      warehouseId: [],
      position: [],
      query: {
        id: '',
      },
      locationDialog: false,
      locations: [],
      options: [{
        value: 'ZTO',
        label: '中通'
      }, {
        value: 'STO',
        label: '申通'
      }, {
        value: 'YTO',
        label: '圆通'
      }],
      logisticsDialog: false,
      reverse: true,
      kd: {}
    }
  },
  methods: {
    formatter(row, column) {
      return row.address;
    },
    //获取货物列表
    async getOutList() {
      this.loading = true;
      const res = await this.$http.get('/goods/enterList');
      for (let i = 0; i < res.obj.length; i++) {
        if (res.obj[i].orderNum === '') {
          res.obj[i].orderNum = '自主生产';
        }
      }
      if (res) {
        this.outList = res.obj;
      }
      this.loading = false;
    },
    enterClose() {
      this.enter = {
        tNote: '',
        tid: '',
        tid1: '',
        orderNum: '',
        name: '',
        type: '',
        count: '',
        countQ: 0,
        countD: 0,
        pid: [],
        area1: '',
        area2: '',
        lid: '',
      };
      this.query.id = '';
      this.warehouseId = '';
    },
    openEnter() {
      this.enterDialog = true;
      // this.shipDialog = true;
    },
    async openShip(item) {
      console.log(item);
      if (item.expNo === null) {
        this.shipDialog = true;
        this.ship.id = item.id;
      } else {
        this.logisticsDialog = true;
        const res = await this.$http.post('/goods/ship', {
          expCode: item.expCode,
          expNo: item.expNo,
        });
        console.log(res);
        this.kd = JSON.parse(res.obj);
      }

    },
    /*enterYes() {
      const pid = this.enter.pid;
      const lid = [];
      for (let i = 0; i < pid.length; i++) {
        lid[i] = pid[i][pid[i].length - 1];
      }
      this.enter.lid = lid.join(',');
      console.log(this.enter);
      this.$refs.enterRef.validate(async valid => {
        const res = await this.$http.post('/warehouse/enter', this.enter);
        if (res) {
          this.getEnterList();
          this.enterDialog = false;
        } else return;

        // }

      })

    },*/

    //远程查询RFID编号
    async querySearchAsync1(queryString, cb) {
      this.queryInfo1.name = queryString;
      const res = await this.$http.get('/warehouse/RFIDProduct/', {params: this.queryInfo1});
      for (let i of res.obj) {
        i.value = i.num + ':' + i.name + '(数量:' + i.count + '单号' + i.orderNum + ')';
        // i.value = i.data;
      }
      cb(res.obj);
    },
    handleSelect1(item) {
      this.enter.tid = item.id;
      this.ship.id = item.id;
      this.enter.area1 = item.num;
      if (item.orderNum === '') {
        this.enter.orderNum = '自主生产';
      } else {
        this.enter.orderNum = item.orderNum;
      }
      this.enter.name = item.name;
      this.enter.count = item.count;
    },

    async getPosition() {
      console.log(this.query.id);
      const res = await this.$http.get('/warehouse/position/', {params: this.query});
      this.position = res.obj;
      console.log(res);
    },

    //提交出库
    async shipYes() {
      this.ship.id=this.enter.id;
      console.log('-----'+this.ship.id);
      const res = await this.$http.put('/goods/out', this.ship);
      this.shipDialog = false;
      if (res) {
        this.getOutList();
      }
    },
    async shipYes1() {
      //console.log(this.ship);
      //console.log("666666");
      //console.log(this.enter);
      // this.ship.expCode;
      this.ship.id=this.enter.tid;
      const res = await this.$http.put('/goods/out', this.ship);
      this.shipDialog = false;
      if (res) {
        this.getOutList();
      }
    }
  },
  created() {
    this.enterDialog = true;
    this.getOutList();
  }
}
</script>

<style scoped>

</style>
